package com.xy.novelmanagement.controller;

import com.xy.model.Novel;
import com.xy.model.PageList;
import com.xy.model.RestfulResult;
import com.xy.model.RootNovelType;
import com.xy.novelmanagement.service.INovelManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/novelmanagement")
public class NovelManagementController {
		@Autowired
		private INovelManagementService novelManagementService;

		@GetMapping(value = "/")
		public RestfulResult getList(Integer pageNum, Integer pageSize) {
				PageList<Novel> list = novelManagementService.getAll(pageNum, pageSize);
				log.info("NovelManagementController getList list = " + list);
				return RestfulResult.success(list);
		}

		@GetMapping(value = "/all/")
		public RestfulResult getAll() {
				List<Novel> list = novelManagementService.selectAll();
				log.info("NovelManagementController getAll allList = " + list);
				return RestfulResult.success(list);
		}

		@GetMapping(value = "/novelType/")
		public RestfulResult getNovelTypeList() {
				List<RootNovelType> typeList = novelManagementService.getTypeList();
				log.info("NovelManagementController getNovelTypeList typeList = " + typeList);
				return RestfulResult.success(typeList);
		}

		@PutMapping(value = "/downShelf/{names}")
		public RestfulResult downShelfBook(@PathVariable String names) {
				log.info("NovelManagementController downShelfBook names = " + names);
				if (names == null || names.equals("")) {
						return RestfulResult.failed("-403", "选择下架的小说至少一本！");
				} else {
						String[] nameList = {};
						if (names.contains(",")) {
								nameList = names.split(",");
								if (nameList.length == 0) {
										return RestfulResult.failed("-403", "选择下架的小说至少一本！");
								}
						} else {
								nameList = new String[]{names};
						}
						log.info("NovelManagementController downShelfBook nameList = " + nameList.length);
						int rest = novelManagementService.downShelfNovel(nameList);
						log.info("NovelManagementController downShelfBook rest = " + rest);
						if (rest == 0) {
								return RestfulResult.failed("-200", "小说不存在");
						}
						return RestfulResult.success(rest);
				}

		}

		@PutMapping(value = "/recover/{names}")
		public RestfulResult recoverBook(@PathVariable String names) {
				log.info("NovelManagementController recoverBook names = " + names);
				if (names == null || names.equals("")) {
						return RestfulResult.failed("-403", "选择下架的小说至少一本！");
				} else {
						String[] nameList = {};
						if (names.contains(",")) {
								nameList = names.split(",");
								if (nameList.length == 0) {
										return RestfulResult.failed("-403", "选择下架的小说至少一本！");
								}
						} else {
								nameList = new String[]{names};
						}
						log.info("NovelManagementController recoverBook nameList = " + nameList.length);
						int rest = novelManagementService.recoverNovel(nameList);
						log.info("NovelManagementController recoverBook rest = " + rest);
						if (rest == 0) {
								return RestfulResult.failed("-200", "小说不存在");
						}
						return RestfulResult.success(rest);
				}
		}

		@PutMapping(value = "/editNovel/{bookName}", produces = "application/json;charset=UTF-8")
		public RestfulResult updateNovel(@PathVariable String bookName, @RequestBody Novel novel) {
				log.info("NovelManagementController updateNovel novel = " + novel);
				log.info("NovelManagementController updateNovel bookName = " + bookName);
				if (bookName == null || bookName.equals("")) {
						return RestfulResult.failed("-403", "小说名不能为空！");
				} else if (novel == null || novel.getBookType() == null || novel.getBookType().size() == 0) {
						return RestfulResult.failed("-403", "小说小说类型至少选一项！");
				}
				novelManagementService.updateNovelType(novel);
				return RestfulResult.success();
		}
}
